package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Tag(name = "01-用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/reg")
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order=10)
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("注册业务,参数" + userRegParam);
        String username = userRegParam.getUsername();
        int count = userMapper.selectByUsername(username);
        if (count>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("实体类:" + user);
        int i = userMapper.insertUser(user);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.UKNOWN_ERROP);
    }

    @PostMapping("/login")
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("登录业务,参数:" + userLoginParam);
        /*
            1.查询用户名和密码是否正确[SELECT查询接口];
            2.判断查询结果
              2.1 登录成功:返回JsonResult对象,包含用户信息userVO;
              2.2 登录失败:返回JsonResult对象;
         */
        //SELECT id,username FROM user WHERE username = ? AND password = ?
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO == null){ //用户名或密码错误
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //在服务器的内存中保存该用户的会话标识;
        //服务器内存: {"user": {"id":100, "username":"lucy"}}
        session.setAttribute("user", userVO);
        //登录成功
        return JsonResult.ok(userVO);
    }

    @GetMapping("/currentUser")
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        //服务端内存中获取会话;
        UserVO userVO = (UserVO) session.getAttribute("user");

        //情况1:用户未登录 ---> {"state":2000,"message":"操作成功","data":null}
        //情况2:用户已登录 ---> {"state":2000,"message":"操作成功","data":{"id":100,"username":"lucy"}}
        /*
            前端代码:
            前端:response.data.data: 获取userVO
            if (response.data.data == null){
                重定向到登录页面;
            }else {
                从userVO中获取username的值;
                页面中显示用户名:欢迎你,xxx
            }
         */
        return JsonResult.ok(userVO);
    }
    @GetMapping("/logout")
    @Operation(summary = "退出登录功能")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }


    }

